package com.yanqu.road.dao.impl.activity.suoyao;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.suoyao.config.SuoYaoRewardConfig;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SuoYaoRewardConfigDaoImpl extends TempDao {

    public static String getSuoYaoRewardConfigMapSql = "select * from t_s_activity_suoyao_reward where activityId=?";

    public Map<Integer, Map<Integer, SuoYaoRewardConfig>> getSuoYaoRewardConfigMap(int activityId) {
        Map<Integer, Map<Integer, SuoYaoRewardConfig>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getSuoYaoRewardConfigMapSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                SuoYaoRewardConfig suoYaoRewardConfig = new SuoYaoRewardConfig();
                suoYaoRewardConfig.setActivityId(rs.getInt("activityId"));
                suoYaoRewardConfig.setRewardId(rs.getInt("rewardId"));
                suoYaoRewardConfig.setFloorNo(rs.getInt("floorNo"));
                suoYaoRewardConfig.setReward(rs.getString("reward"));
                suoYaoRewardConfig.setScore(rs.getInt("score"));
                suoYaoRewardConfig.setWeight(rs.getInt("weight"));
                suoYaoRewardConfig.setBigReward(rs.getBoolean("isBigReward"));
                suoYaoRewardConfig.setSort(rs.getInt("sort"));
                if (!map.containsKey(suoYaoRewardConfig.getFloorNo())) {
                    map.put(suoYaoRewardConfig.getFloorNo(), new ConcurrentHashMap<>());
                }
                map.get(suoYaoRewardConfig.getFloorNo()).put(suoYaoRewardConfig.getRewardId(), suoYaoRewardConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

}
